package com.ruyuan.rapid.common.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Data
@Slf4j
public class Rule implements Comparable<Rule>, Serializable {

    /**
     * 规则id,全局唯一
     */
    private String id;
    /**
     * 规则名称
     */
    private String name;
    /**
     * 规则对应的协议
     */
    private String protocol;
    /**
     * 规则排序, 用于以后外衣有需求做一个路径绑定多个规则,但是最终只能执行一个规则, 用于做优先级判断
     */
    private Integer order;

    private Set<FilterConfig> filterConfigs = new HashSet<>();

    /**
     * 向规则添加指定的过滤器
     * @param config
     * @return
     */
    public boolean addFilterConfig(FilterConfig config) {
        return filterConfigs.add(config);
    }

    /**
     * 通过指定的id获取FilterConfig
     * @param id
     * @return
     */
    public FilterConfig getFilterConfig(String id) {
        for (FilterConfig filterConfig : filterConfigs) {
            if (StringUtils.equalsIgnoreCase(filterConfig.getId(), id)) {
                return filterConfig;
            }
        }
        return null;
    }

    /**
     * 根据传入的filterId,判断当前rule中是否存在
     * @param id
     * @return
     */
    public boolean hasId(String id) {
        for (FilterConfig filterConfig : filterConfigs) {
            if (StringUtils.equalsIgnoreCase(filterConfig.getId(), id)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int compareTo(Rule o) {
        int orderCompare = Integer.compare(getOrder(), o.getOrder());
        if (orderCompare == 0) {
            return getId().compareTo(o.getId());
        }
        return orderCompare;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Rule rule = (Rule) o;
        return Objects.equals(id, rule.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    /**
     * 过滤器的配置类
     */
    public static class FilterConfig {
        /**
         * 过滤器的唯一ID
         */
        private String id;

        /**
         * 过滤器的配置信息描述定义: json string {timeout:500} {balance:rr}
         */
        private String config;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getConfig() {
            return config;
        }

        public void setConfig(String config) {
            this.config = config;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            FilterConfig that = (FilterConfig) o;
            return Objects.equals(id, that.id);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, config);
        }
    }
}
